package com.yowasa.xiuxian.engine.hone;

import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.Mental;
import com.yowasa.xiuxian.entity.bo.DescInterface;
import com.yowasa.xiuxian.entity.bo.ElementInterface;
import com.yowasa.xiuxian.entity.bo.LimitCheckInterface;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.bo.user.UserBattle;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.util.JSONUtils;
import org.apache.commons.lang3.tuple.Pair;


import java.util.List;
import java.util.Map;

public interface MentalInterface extends ElementInterface, LimitCheckInterface, DescInterface {

    //描述
    String desc();

    //写入描述
    default Pair<String, String> getItemDesc() {
        Mental annotation = this.getClass().getAnnotation(Mental.class);
        if (annotation == null) {
            return null;
        }
        String name = annotation.name();
        return Pair.of(name, "[心法]" + name + "\n限制:" + limitDesc() + "\n描述:" + desc());
    }

    ;

    //心法自带的可学习战技
    List<String> bsList();

    //心法自带的可学习神通
    default List<String> specialList(){
        return Lists.newArrayList();
    };

    //预估修炼时间
    default int getExpect(UserBO user) {
        ElementEnum element = getElement();
        Integer code = user.getStage();
        StageEnum level = StageEnum.getById(code);
        int need = level.getNeed();
        Integer lingGen = user.getElement().get(element.getName());
        return ((100 - user.getLevel()) * need) * 200 / (user.getUserAttr().getInts() * lingGen);
    }


    //修炼获取经验
    default int getExp(UserBO user, int days) {
        ElementEnum element = getElement();
        Integer lingGen = user.getElement().get(element.getName());
        int exp = user.getUserAttr().getInts() * days * lingGen / 200;
        return exp;
    }

    //测算学习所需时间
    default int studyTime(UserBO user) {
        int und = user.getUserAttr().getUnd();
        return 1000 / und;
    }

    //突破
    String breakUp(UserBO user);

    //获取基础提升
    default UserBattle getBaseUp(UserBO user) {
        Integer code = user.getStage();
        StageEnum stage = StageEnum.getById(code);
        Integer level = user.getLevel();
        if (level < 100) {
            throw new HandlerException("你当前境界还没有修炼至圆满");
        }
        user.setStage(code + 1);
        user.setLevel(0);
        //计算突破要提升的属性值
        UserBattle upBattle = UserBattle.genUpBattleAttr(user.getUserAttr(), code);
        if (stage == StageEnum.FAN_REN) {
            upBattle.setSpd(upBattle.getSpd() + 100);
        }
        //寿命提升
        user.getUserConfig().setLife(user.getUserConfig().getLife() + StageEnum.getById(code + 1).getAddLife());
        //涅槃特殊提升
        if (user.getUserConfig().getStudiedSpecialEffect().contains("涅槃之法")) {
            if (user.getUserAttr().getCon() < 70) {
                user.getUserAttr().setCon(user.getUserAttr().getCon() + 3);
            }
            if (user.getUserAttr().getStr() < 70) {
                user.getUserAttr().setStr(user.getUserAttr().getStr() + 3);
            }
            if (user.getUserAttr().getInts() < 70) {
                user.getUserAttr().setInts(user.getUserAttr().getInts() + 3);
            }
            if (user.getUserAttr().getDex() < 70) {
                user.getUserAttr().setDex(user.getUserAttr().getDex() + 3);
            }
        }

        return upBattle;
    }

    //构建提升后的战斗参数
    default void buildNewUpBattle(UserBO user, UserBattle upBattle) {
        //源生战斗数值
        String battleStr = user.getOriginBattle();
        Map<String, Integer> battleMap = JSONUtils.toObject(battleStr, Map.class);
        String upBattleStr = JSONUtils.toJson(upBattle);
        Map<String, Integer> upBattleMap = JSONUtils.toObject(upBattleStr, Map.class);
        //将源生数值与提升数值相加
        for (Map.Entry<String, Integer> entry : battleMap.entrySet()) {
            entry.setValue(entry.getValue() + upBattleMap.get(entry.getKey()));
        }
        String newBattleStr = JSONUtils.toJson(battleMap);
        UserBattle newBattle = JSONUtils.toObject(newBattleStr, UserBattle.class);
        //保存
        user.setUserOriginBattle(newBattle);

    }


}
